﻿using AutoMapper;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using Zocono.Infrastructure.Common;
using Zocono.WCS.Domain.DomainRepository;
using Zocono.WCS.Domain.EntityInfo;
using Zocono.WCS.Infrastructure.ApllicationDto;
using Zocono.WCS.Infrastructure.ApllicationDto.DtoWcsManage;

namespace Zocono.WCS.Domain.EquipmentManageDomain
{
    public class EquipmentTypeService : IEquipmentTypeService
    {
        private readonly ILogUtil _logUtil;
        private readonly IEquipmentTypeRepository _EquipmentTypeRepository;
        private readonly IMapper _Mapper;
        private readonly ISnowflake _Snowflake;


        public EquipmentTypeService(ISnowflake snowflake, IEquipmentTypeRepository EquipmentTypeRepository, ILogUtil logUtil, IMapper mapper)
        {
            _EquipmentTypeRepository = EquipmentTypeRepository;
            _logUtil = logUtil;
            _Mapper = mapper;
            _Snowflake = snowflake;
        }

        public async Task<long> AddEquipmentTypeAsync(EquipmentTypeInfoDto EquipmentTypeInfoDto)
        {
            var EquipmentTypeInfo = _Mapper.Map<EquipmentTypeInfoDto, EquipmentTypeInfo>(EquipmentTypeInfoDto);
            long id = _Snowflake.GetId();
            EquipmentTypeInfo.ID = id;
            bool result = await _EquipmentTypeRepository.InsertAsync(EquipmentTypeInfo);
            return result ? id : 0;
        }

        public async Task<bool> DeleteEquipmentTypeAsync(long ID)
        {
            bool result = await _EquipmentTypeRepository.DeleteAsync(it => it.ID == ID);
            return result;
        }

        public async Task<bool> UpdateEquipmentTypeAsync(EquipmentTypeInfoDto EquipmentTypeInfoDto)
        {
            var equipmentTypeInfo = _Mapper.Map<EquipmentTypeInfoDto, EquipmentTypeInfo>(EquipmentTypeInfoDto);
            bool result = await _EquipmentTypeRepository.UpdateAsync(equipmentTypeInfo);
            return result;
        }

        public async Task<List<EquipmentTypeInfoDto>> GetEquipmentTypeAsync(QueryEquipmentTypeInfoDto queryEquipmentTypeInfoDto)
        {
            var isDispatch = queryEquipmentTypeInfoDto.IsDispatch;
            var typeCode = queryEquipmentTypeInfoDto.TypeCode;
            var typeName = queryEquipmentTypeInfoDto.TypeName;
            var exp = Expressionable.Create<EquipmentTypeInfo>();
            exp.AndIF(!string.IsNullOrEmpty(typeCode), (a) => a.TypeCode.Contains(typeCode));
            exp.AndIF(!string.IsNullOrEmpty(typeName), (a) => a.TypeName.Contains(typeName) );
            exp.AndIF(isDispatch.HasValue, (a) => a.IsDispatch == isDispatch);
            var result = await _EquipmentTypeRepository.GetListAsync(exp.ToExpression());
            List<EquipmentTypeInfoDto> list = new List<EquipmentTypeInfoDto>();
            _Mapper.Map(result, list);
            return list;
        }

        public async Task<EquipmentTypeInfoDto> GetEquipmentTypeByIDAsync(long ID)
        {
            var result = await _EquipmentTypeRepository.GetByIdAsync(ID);
            var vModel = _Mapper.Map<EquipmentTypeInfo, EquipmentTypeInfoDto>(result);
            return vModel;
        }

        public async Task<List<EquipmentTypeInfoDto>> GetAllEquipmentTypeAsync()
        {
            List<EquipmentTypeInfoDto> equipmentTypeInfoDtos = new List<EquipmentTypeInfoDto>();
            List<EquipmentTypeInfo> equipmentTypeInfos = await _EquipmentTypeRepository.GetListAsync();
            _Mapper.Map(equipmentTypeInfos, equipmentTypeInfoDtos);
            return equipmentTypeInfoDtos;
        }
    }
}
